<?php

namespace App\Admin\Controllers;

use App\Models\Configs;
use Carbon\Carbon;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class ConfigImagesController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '图片设置';


    protected $valueType = [
        'image' => '图片',
        'text' => '文本'
    ];

    protected $type = [
        'banner' => '轮播图',
        'qrcode' => '企业二维码',
        'honour' => '荣誉证书'
    ];

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Configs());

        $grid->model()->where('value_type', 'image');

        $grid->column('id', '编号')->sortable()->width(150);
        $type = $this->type;
        $grid->column('type', '数据类型')->width(250)->display(function ($time) use ($type) {
            return  $type[$time];
        })->label([
            'banner' => 'danger',
            'qrcode' => 'warning',
            'honour'=> 'success',
        ]);
        $grid->column('value', '图片')->image();
        $grid->column('created_at', '创建时间')->width(200)->display(function ($time) {
            return Carbon::parse($time)->setTimezone('Asia/Shanghai')->toDateTimeString();
        });
        $grid->column('updated_at', '修改时间')->width(200)->display(function ($time) {
            return Carbon::parse($time)->setTimezone('Asia/Shanghai')->toDateTimeString();
        });

        $grid->filter(function($filter){
            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            $filter->equal('type', '数据类型')->select($this->type);
        });
        $grid->disableRowSelector();
        $grid->disableExport();
        $grid->actions(function ($actions) {
            $actions->disableView();
            $actions->disableDelete();
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed   $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Configs::findOrFail($id));

        $show->field('key', __('Key'));
        $show->field('value', '图片')->image();
        $show->field('type', __('Type'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Configs());

        $form->hidden('key')->default(time());
        $type = $this->type;
        $form->select('type', '数据类型')->options($type)->required();
        $form->hidden('value_type')->default('image');

        $path = 'images/admin/config';
        $form->image('value', '图片')->required()->help('上传前请压缩，上传规格 1920 * 600')->uniqueName()->move($path);

        $form->text('describe', '说明')->readonly()->default(function ($form) {
            return $this->type[$form->model()->type] ?? '';
        });

        $form->saving(function (Form $form) {
            $form->describe = $this->type[$form->type] ?? '';
        });

        $form->footer(function ($footer) {
            // 去掉`重置`按钮
            $footer->disableReset();
            // 去掉`查看`checkbox
            $footer->disableViewCheck();
            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();
            // 去掉`继续创建`checkbox
            $footer->disableCreatingCheck();
        });
        $form->tools(function ($tools){
            // 去掉`查看`按钮
            $tools->disableView();
        });
        return $form;
    }
}
